package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.OrderGoodsMapper;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author pengyehua
 * @date 2020/12/6 21:33
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService{

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    /**
     * 查询指定订单
     * @param orderBO
     * @return
     */
    @Override
    public ListData<Order> queryOrder(OrderBO orderBO) {
        //分页
        PageHelper.startPage(orderBO.getPage(),orderBO.getLimit());

        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();

        //排序
        orderExample.setOrderByClause(orderBO.getSort() + " " + orderBO.getOrder());

        //查询条件
        if(orderBO.getUserId() != null){
            criteria.andUserIdEqualTo(orderBO.getUserId());
        }
        if(orderBO.getOrderSn() != null && !orderBO.getOrderSn().equals("")){
            criteria.andOrderSnEqualTo(orderBO.getOrderSn());
        }
        if(orderBO.getOrderStatusArray() != null){
                criteria.andOrderStatusIn(orderBO.getOrderStatusArray());
        }
        //被删除的订单不能被查询
        criteria.andDeletedEqualTo(false);
        List<Order> orders = orderMapper.selectByExample(orderExample);

        ListData<Order> orderListData = new ListData<>();
        orderListData.setItems(orders);

        //获取查询结果的条目总数
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        int total =(int) orderPageInfo.getTotal();
        orderListData.setTotal(total);
        return orderListData;
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @Override
    public OrderDetail queryOrderDetail(Integer id) {
        //根据orderId查询order表
        Order order = orderMapper.selectByPrimaryKey(id);
        //根据userId查询user表
        User user = userMapper.selectByPrimaryKey(order.getUserId());
        //根据orderId查询order_goods表
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        //封装到orderDetail中
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrder(order);
        orderDetail.setUser(user);
        orderDetail.setOrderGoods(orderGoods);
        return orderDetail;
    }

    /**
     * 处理发货
     * @param map
     */
    @Override
    public void updateOrderShip(Map map) {
        Order order = new Order();
        Date date = new Date();
        order.setShipTime(date);
        order.setShipChannel((String) map.get("shipChannel"));
        order.setShipSn((String) map.get("shipSn"));
        order.setId((int) map.get("orderId"));
        order.setOrderStatus((short) 301);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 处理退款
     * @param map
     */
    @Override
    public void updateOrderStatus(Map map) {
        Order order = new Order();
        order.setId((int)map.get("orderId"));
        order.setOrderStatus((short) 203);
        orderMapper.updateByPrimaryKeySelective(order);
    }
}
